#!/usr/bin/env bash
set -ex

cd $(dirname $0)/..

source ./scripts/version.sh

if [ -z "${GODEBUG}" ]; then
    EXTRA_LDFLAGS="${EXTRA_LDFLAGS} -w"
    DEBUG_GO_GCFLAGS=""
    DEBUG_TAGS=""
else
    DEBUG_GO_GCFLAGS='-gcflags=all=-N -l'
fi

REVISION=$(git rev-parse HEAD)$(if ! git diff --no-ext-diff --quiet --exit-code; then echo .dirty; fi)
RELEASE=${PROG}-windows.amd64

BUILDTAGS="netgo osusergo no_stage static_build sqlite_omit_load_extension"
GO_BUILDTAGS="${GO_BUILDTAGS} ${BUILDTAGS} ${DEBUG_TAGS}"

VERSION_FLAGS="
        -X ${K3S_PKG}/pkg/version.GitCommit=${REVISION}
        -X ${K3S_PKG}/pkg/version.Program=${PROG}
        -X ${K3S_PKG}/pkg/version.Version=${VERSION}
        -X ${RKE2_PKG}/pkg/images.DefaultRegistry=${REGISTRY}
        -X ${RKE2_PKG}/pkg/images.DefaultEtcdImage=rancher/hardened-etcd:${ETCD_VERSION}-build20210915
        -X ${RKE2_PKG}/pkg/images.DefaultKubernetesImage=${REPO}/hardened-kubernetes:${KUBERNETES_IMAGE_TAG}
        -X ${RKE2_PKG}/pkg/images.DefaultPauseImage=rancher/pause:${PAUSE_VERSION}
        -X ${RKE2_PKG}/pkg/images.DefaultRuntimeImage=${REPO}/${PROG}-runtime:${DOCKERIZED_VERSION}
"

GO_LDFLAGS="${STATIC_FLAGS} ${EXTRA_LDFLAGS}"
echo ${DEBUG_GO_GCFLAGS}
GOOS=windows \
  CGO_ENABLED=1 CXX=x86_64-w64-mingw32-g++ CC=x86_64-w64-mingw32-gcc \
  go build \
    -tags "${GO_BUILDTAGS}" \
    ${GO_GCFLAGS} ${GO_BUILD_FLAGS} \
    -o bin/${PROG}.exe \
    -ldflags "${GO_LDFLAGS} ${VERSION_FLAGS}" \
    ${GO_TAGS}
